const db = require('../model/database')
const query = require('../model/query')
const jwt = require('../util/jwt')
const Jwt = require('jsonwebtoken')
const { jwtSecretKey } = require('../config/config')


// 登录
// exports.login = async (req, res) => {
//   const userInfo = req.body
//   // 确认登录密码是否正确
//   const sql = 'select * from admin where username=?'
//   let result = await query(sql, userInfo.username, db)
//   if (result.length > 0) {
//     // 排除密码
//     const user = { ...result[0], password: '' }
//     console.log(user,'2255533');
//     const token = 'Bearer ' + await jwt.sign(user, jwtSecretKey, { expiresIn: '24h' })
//     result[0].password === userInfo.password ? res.send({ code: 20000, msg: '登陆成功', data: { token } }) : res.send({ code: 40000, msg: '密码错误,登录失败' })
//   } else {
//     res.send({ code: 40000, msg: '用户不存在' })
//   }
// }

//学生登录
exports.login = async (req, res) => {
  const userInfo = req.body
  // 确认登录密码是否正确
  const sql = 'select * from admin where username=?'
  let result = await query(sql, userInfo.username, db)
  
  const sql1 = 'select id,name as username,id_number as password,avatar,class as classid from student where id_number=?'
  let result1 = await query(sql1, userInfo.username, db)
  
  if (result.length > 0 || result1.length > 0) {
    // 管理员登录
    if(result.length > 0){
      const user = { ...result[0], password: '' }
      const token = 'Bearer ' + await jwt.sign(user, jwtSecretKey, { expiresIn: '24h' })//24h
      result[0].password === userInfo.password ? res.send({ code: 20000, msg: '登陆成功', data: { token } }) : res.send({ code: 40000, msg: '密码错误,登录失败' })
    }
    //学生登录
    if(result1.length > 0){
      result1[0].password = result1[0].password.substr(-6)
      const user = { ...result1[0], password: '',roles:'学生' } 
      const token = 'Bearer ' + await jwt.sign(user, jwtSecretKey, { expiresIn: '24h' })
      result1[0].password === userInfo.password ? res.send({ code: 20000, msg: '登陆成功', data: { token } }) : res.send({ code: 40000, msg: '密码错误,登录失败' })
    }
  } else {
    res.send({ code: 40000, msg: '用户不存在' })
  }
}


// 获取用户信息
exports.adminInfo = (req, res) => {
  // 去除bearer
  const token = req.query.token.split(' ')[1]
  let user = {}
  //解密token
  Jwt.verify(token, jwtSecretKey, (error, authData) => {
    if (error) {
      res.send({ code: 40000, msg: 'token过期' })
    } else {
      user = authData;
      delete user.password
      res.send({ code: 20000, msg: '登陆成功', data: { ...user } })
    }
  })
}



// 退出登录
exports.logout = (req, res) => {
  res.send({ code: 20000, msg: '退出成功' })
}

// 修改密码
exports.updatePassword = async (req, res) => {
  const { oldPassword, newPassword, confirmPassword } = req.body
  // 解析token
  const token = req.headers.authorization.split(' ')[1]
  let user = {}
  //解密token
  Jwt.verify(token, jwtSecretKey, (error, authData) => {
    if (error) {
      res.send({ code: 40000, msg: 'token过期' })
    } else {
      user = authData;
    }
  })

  const sql = 'select * from admin where id=?'
  let result = await query(sql, user.id, db)
  if (result.length > 0) {
    if (result[0].password === oldPassword) {
      const sql = 'update admin set password=? where id=?'
      await query(sql, [newPassword, user.id], db)
      res.send({ code: 20000, msg: '修改成功' })
    } else {
      res.send({ code: 40000, msg: '原密码错误' })
    }
  } else {
    res.send({ code: 40000, msg: '用户不存在' })
  }
}

